﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XBLIGUK2011Talk2.HUD
{
    public class HUDSlider : HUDObject
    {
        public float Min;
        public float Max;
        public string Text;
        protected float val;

        public OnChangeEvent OnChange;

        public float Value
        {
            get { return val; }
            set 
            {
                val = MathHelper.Clamp(value, Min, Max);
                if (OnChange != null)
                    OnChange(this);
            }
        }

        public Color Shadow = Color.MediumPurple;
        SpriteFont font;
        float slideValue;

        public int Height = 16;
        int width = 8;

        public HUDSlider(string name, string text, int top, int left,float min,float max) : base( name, top, left)
        {
            Min = min;
            Max = max;

            Text = text;

            Shadow.A = (byte)(255 * .25f);
        }
        public override void Update(GameTime gameTime)
        {
            slideValue = MathHelper.Lerp(4, 116, val / (Max));
            bounds = new Rectangle(Left + (int)slideValue, Top + (int)Parent.Top + font.LineSpacing, width, Height);
            base.Update(gameTime);

            if (dragging)
            {
                Value += (delta.X * Max) / 128f;
                //Value = MathHelper.Lerp(Min, Max, (float)(thisMState.X - Left) / 128f);
                Game.Window.Title = delta.ToString();
            }
        }
        public override void Draw(GameTime gameTime)
        {
            if (font == null)
                font = Game.Content.Load<SpriteFont>("Fonts/hudFont");

            spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/HUD/HUDSliderBackground"), new Rectangle(Left, Top + (int)Parent.Top + font.LineSpacing, 128, Height), Color.White);
            spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/HUD/HUDSliderToggle"), new Rectangle(Left + (int)slideValue, Top + (int)Parent.Top + font.LineSpacing, width, Height), Color.White);
            

            spriteBatch.DrawString(font, Text, new Vector2((Left + 62) - font.MeasureString(Text).X / 2, Top + (int)Parent.Top - (font.LineSpacing / 10) + 2), Shadow);
            spriteBatch.DrawString(font, Text, new Vector2((Left + 64) - font.MeasureString(Text).X / 2, Top + (int)Parent.Top - (font.LineSpacing / 10)), Color.Purple);

            spriteBatch.DrawString(font, val.ToString(), new Vector2(Left + 139, Top + (int)Parent.Top + (font.LineSpacing / 1.125f)+1), Color.Black);
            spriteBatch.DrawString(font, val.ToString(), new Vector2(Left + 140, Top + (int)Parent.Top + (font.LineSpacing / 1.125f)), Color.White);
        }
    }
}
